|
Server : Apache System : Linux server.mata-lashes.com 3.10.0-1160.90.1.el7.x86_64 #1 SMP Thu May 4 15:21:22 UTC 2023 x86_64 User : matalashes ( 1004) PHP Version : 8.1.29 Disable Function : NONE Directory : /home/matalashes/www/wp-content/plugins/security-malware-firewall/lib/CleantalkSP/Common/ |
Upload File : |
<?php
namespace CleantalkSP\Common;
/**
* @psalm-suppress UnusedClass
*/
class DNS
{
private static $min_server_timeout = 500;
private static $max_server_timeout = 1500;
private static $default_server_ttl;
/**
* Function DNS request
*
* @param string $host URL
* @param bool $return_first returns only first found IP, HOST, TTL
* @param null|int $type DNS type name
*
* @return array|null
*/
public static function getRecord($host, $return_first = false, $type = null)
{
$servers = array(
"ip" => null,
"host" => $host,
"ttl" => static::$default_server_ttl
);
// Get DNS records about URL
if (function_exists('dns_get_record')) {
// Localhost generates errors. block these by @
$records = $type
? @dns_get_record($host, $type)
: @dns_get_record($host);
if ($records !== false) {
$servers = array();
foreach ($records as $server) {
$servers[] = $server;
}
}
}
// Another try if first failed
if (function_exists('gethostbynamel') && empty($records)) {
$records = gethostbynamel($host);
if ($records !== false) {
$servers = array();
foreach ($records as $server) {
$servers[] = array(
"ip" => $server,
"host" => $host,
"ttl" => static::$default_server_ttl
);
}
}
}
return $return_first
? reset($servers)
: $servers;
}
/**
* @param $servers
*
* @return array|null
* @psalm-suppress PossiblyUnusedMethod
*/
public static function findFastestServer($servers)
{
$tmp = null;
$fast_server_found = false;
foreach ($servers as $server) {
if ($fast_server_found) {
$ping = static::$max_server_timeout;
} else {
$ping = static::getResponseTime($server['ip']);
$ping *= 1000;
}
$tmp[ $ping ] = $server;
$fast_server_found = $ping < static::$min_server_timeout;
}
ksort($tmp);
$response = $tmp;
return $response ?: null;
}
/**
* Function to check response time
*
* @param string URL
*
* @return int|float Response time
*/
public static function getResponseTime($host)
{
// Skip localhost ping cause it raise error at fsockopen.
// And return minimum value
if ($host === 'localhost') {
return 0.001;
}
$starttime = microtime(true);
$file = @fsockopen($host, 80, $errno, $errstr, static::$max_server_timeout / 1000);
$stoptime = microtime(true);
if (! $file) {
$status = static::$max_server_timeout / 1000; // Site is down
} else {
fclose($file);
$status = ($stoptime - $starttime);
$status = round($status, 4);
}
return $status;
}
/**
* Get server TTL
* Wrapper for self::getRecord()
*
* @param $host
*
* @return mixed|null
* @psalm-suppress PossiblyUnusedMethod
*/
public static function getServerTTL($host)
{
$server = static::getRecord($host, true);
return $server['ttl'];
}
}